<template>
  <div class="config-section">
    <h3>标题文本</h3>
    
    <div class="input-group">
      <div class="input-item">
        <span>标题内容</span><input type="text" placeholder="请输入标题" v-model="title" @input="handlerInput('title',title)">
      </div>
      <div class="input-item">
        <span>副标题内容</span><input type="text" placeholder="请输入副标题" v-model="subtitle" @input="handlerInput('subtitle',subtitle)">
      </div>
    </div>
    <div>
      <div class="flex-between">
        <span>显示位置</span>
        <div>
          <button :class="{ 'active': activeButton === 'left' }" 
                  @click="selectOption('textAlign', 'left')">居左
          </button>
          <button :class="{ 'active': activeButton === 'center' }" 
                  @click="selectOption('textAlign', 'center')">居中
          </button>
          <button :class="{ 'active': activeButton === 'right' }" 
                  @click="selectOption('textAlign', 'right')">居右
          </button>
        </div>
      </div>
      <div class="flex-between">
        <span>标题大小</span>
        <div>
          <button :class="{ 'active': titleSize === '16' }" 
                  @click="selectOption('titleSize', '16')">16号</button>
          <button :class="{ 'active': titleSize === '14' }" 
                  @click="selectOption('titleSize', '14')">14号</button>
          <button :class="{ 'active': titleSize === '12' }" 
                  @click="selectOption('titleSize', '12')">12号</button>
        </div>
      </div>
      <div class="flex-between">
        <span>副标题大小</span>
        <div>
          <button :class="{ 'active': subTitleSize === '12' }" 
                  @click="selectOption('subTitleSize', '12')">12号</button>
          <button :class="{ 'active': subTitleSize === '14' }" 
                  @click="selectOption('subTitleSize', '14')">14号</button>
          <button :class="{ 'active': subTitleSize === '16' }" 
                  @click="selectOption('subTitleSize', '16')">16号</button>
        </div>
      </div>
      <div class="flex-between">
        <span>标题粗细</span>
        <div>
          <button :class="{ 'active': titleWeight === 'bold' }" 
                  @click="selectOption('titleWeight', 'bold')">加粗体</button>
          <button :class="{ 'active': titleWeight === 'normal' }" 
                  @click="selectOption('titleWeight', 'normal')">常规体</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, watch, defineEmits, defineProps } from 'vue'

const props = defineProps({
  currentData: {
    type: Object,
    default: () => ({})
  },
  hasSelectedBlock: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update-data'])

const activeButton = ref('left')
const titleSize = ref('16')
const subTitleSize = ref('12')
const titleWeight = ref('bold')
const title = ref('')
const subtitle = ref('')

// 监听currentData变化，同步更新本地状态
watch(() => props.currentData, (newData) => {
  if (newData) {
    activeButton.value = newData.textAlign || 'left'
    titleSize.value = newData.titleSize || '16'
    subTitleSize.value = newData.subTitleSize || '12'
    titleWeight.value = newData.titleWeight || 'bold'
    title.value = newData.title || ''
    subtitle.value = newData.subtitle || ''
  }
}, { deep: true, immediate: true })

const handlerInput = (type, value) => {
  emit('update-data', { [type]: value })
}

const selectOption = (type, value) => {
  if (type === 'textAlign') {
    activeButton.value = value
  } else if (type === 'titleSize') {
    titleSize.value = value
  } else if (type === 'subTitleSize') {
    subTitleSize.value = value
  } else if (type === 'titleWeight') {
    titleWeight.value = value
  }
  
  emit('update-data', { [type]: value })
}
</script>
<style scoped>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.config-section {
    background-color: white;
    padding: 10px;
    max-width: 100%;
}

.flex-between {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
    padding: 10px 0;
}

.input-group {
    padding: 20px 0;
    border-bottom: 1px dashed #eee;
}

.flex-between button {
    cursor: pointer;
    border: none;
    padding: 5px 10px;
}

.input-item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.input-item span {
    width: 80px;
    display: inline-block;
}

.input-item input{
    border: none;
    padding: 5px 10px;
    border-bottom: 2px solid #eee;
    margin-left: 10px;
}

.input-item input:focus {
    outline: none;
    border-bottom: 2px solid #000;
}

.active {
    background-color: #ff7a00;
    color: white;
}
</style>